<template>
  <div class="login">
    <div class="login-content">
      <div class="login-form">
        <div class="head">
          <el-link class="title" type="primary" :underline="false"
            >登录 / 智慧党建系统</el-link
          >
        </div>
        <el-form :model="form" :rules="rules" ref="ruleForm">
          <el-form-item prop="userName">
            <el-input
              v-model="form.userName"
              placeholder="输入用户名/手机号"
            ></el-input>
          </el-form-item>
          <el-form-item prop="userPass">
            <el-input
              v-model="form.userPass"
              placeholder="输入密码"
              type="password"
            ></el-input>
          </el-form-item>

          <div class="user-tips">用户名由系统管理员分配，或者是您的手机号</div>

          <div class="btn-box">
            <el-button
              type="primary"
              class="login-btn"
              @click="login('ruleForm')"
              >登 录</el-button
            >
          </div>
        </el-form>
        <div class="foot">
          <el-link class="langu" type="primary" :underline="false"
            >重置账号</el-link
          >
        </div>
      </div>
      <div class="login-register">
        <h2>没有账号？</h2>
        <p>立即注册加入我们，和我们一起开始旅程吧</p>
        <div class="btn-register">注册</div>
      </div>
    </div>
    <div style="clear:both;"></div>
  </div>
</template>
<script>
import loginApi from "@/api/login";
import { Decrypt, Encrypt } from "@/utils/CryptoJS.js";
export default {
  name: "Login",
  data() {
    return {
      form: {
        userPass: "", //密码
        userName: "" //登录账号
      },
      rules: {
        userName: [
          { required: true, message: "输入用户名/手机号", trigger: "blur" }
        ],
        userPass: [{ required: true, message: "输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
    login: function(formName) {
      var that = this;
      this.$refs[formName].validate(valid => {
        if (valid) {
          let loading = this.$loading({
            lock: true,
            text: "登录中",
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.5)"
          });
          var data = {
            username: that.form.userName,
            //password:Encrypt(that.form.userPass)//后台说他们暂时没加密，传明文
            password: that.form.userPass
          };
          that.$http
            .post(loginApi.login, data, "form-data")
            .then(res => {
              //数据处理
              setTimeout(() => {
                loading.close();
              }, 500);
              if (res.data.status == 200) {
                localStorage.clear();
                localStorage.setItem("token", res.data.data.token); //设置token 前端判断后端获取的是cookie
                localStorage.setItem(
                  "userInfo",
                  JSON.stringify(res.data.data.userInfo)
                ); //保存用户信息
                let date = new Date();
                that.$router.push({ path: "/" });
              } else {
                that.$message.error(res.data.retmsg);
              }
            })
            .catch(err => console.log(err));
        } else {
          console.log("error submit!!");
          that.$message.error("请输入登录信息！！！");
          return false;
        }
      });
    }
  }
};
</script>
<style lang="less" scoped>
.login {
  position: fixed;
  min-width: 1000px;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  background-color: #fff;
  //   background: url(../../assets/images/login/login_bg_1.png) no-repeat #fff;
  //   background-size: 62% 100%;
}
.login-content {
  position: fixed;
  left: 50%;
  width: 900px;
  top: 50%;
  margin-top: -250px;
  margin-left: -450px;
  background-color: #ffffff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.2);
  border-radius: 10px;
  overflow: hidden;
}
.login-img {
  width: 500px;
  margin-right: 100px;
  margin-left: 20%;
  vertical-align: middle;
}
.login-form {
  vertical-align: middle;
  float: left;
  width: 450px;
  height: 500px;
}
.login-register {
  width: 450px;
  float: right;
  height: 500px;
  display: flex;
  color: #fff;
  align-items: center;
  padding: 150px 0;
  box-sizing: border-box;
  flex-direction: column;
  background-color: #ff454f;
  background-image: linear-gradient(to bottom right, #ff464c, #ff416b);
}
.login-register h2 {
  font-size: 40px;
  font-weight: 700;
}
.login-register p {
  font-size: 14px;
  margin: 30px 0;
}
.login-register .btn-register {
  width: 120px;
  padding: 10px 20px;
  text-align: center;
  border: 1px solid #fff;
  border-radius: 20px;
  cursor: pointer;
}
.login-register .btn-register:hover {
  background-color: #fff;
  color: #ff454f;
}
.login-form .head {
  width: 100%;
  margin: 70px auto;
  text-align: center;
}
.login-form .head .title {
  font-size: 30px;
  font-weight: 700;
}
.login-form .head .change {
  color: #999999;
  font-size: 16px;
  cursor: pointer;
}
.login-form /deep/.el-form {
  padding: 0 50px;
  box-sizing: border-box;
}
.login-form > input {
  border: none;
  outline: none;
  display: block;
  width: 100%;
  height: 45px;
  line-height: 40px;
  text-indent: 3.6em;
  border-radius: 4px;
  font-size: 12px;
  //   border: 1px solid #ddd;
}
.login-form /deep/.el-input__inner {
  background-color: #eee;
}
//.login-form .user {margin-bottom:36px;background:white url(../images/login_content_user.png) 4.5% 50% no-repeat;margin-bottom:0px;}
.login-form .user-tips {
  height: 40px;
  line-height: 40px;
  color: #999999;
  text-align: left;
  margin-top: -10px;
}
//.login-form .pwd {margin-bottom:40px;background:white url(../images/login_content_pwd.png) 4.5% 50% no-repeat;}
.login-form .btn-box {
  width: 100%;
  text-align: center;
}
.login-form .login-btn {
  width: 120px;
  color: white;
  border-radius: 25px;
  text-align: center;
  font-size: 16px;
  margin: 20px 0;
  cursor: pointer;
}

.login-form .login-btn:hover {
  //   background-color: #136eb4;
  //   border-color: #105a94;
  background-color: #ff454f;
  border-color: #ff454f;
}
.login-form .login-btn[disabled] {
  background-color: #ccc;
  border-color: #ccc;
  opacity: 1;
  color: #aaa;
  cursor: default;
}
.login-form .foot {
  width: 100%;
  text-align: center;
  font-size: 12px;
}
.login-form .foot .langu {
  cursor: pointer;
  margin-left: 10px;
  margin-right: 10px;
}
.login-form .foot .langu:hover {
  text-decoration: underline;
}
.login-form .foot .langu:first-child {
  margin-left: 0px;
}
.login-form-QRCode {
  vertical-align: middle;
  display: inline-block;
  width: 400px;
  height: 360px;
}
.login-form-QRCode .head {
  width: 100%;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  margin-bottom: 60px;
}
.login-form-QRCode .head .title {
  color: #188ae2;
  font-size: 22px;
}
.login-form-QRCode .head .change {
  color: #999999;
  font-size: 16px;
  cursor: pointer;
}
.login-form-QRCode .qrcode-img {
  position: relative;
  margin: 20px auto;
  opacity: 1;
  width: 200px;
  height: 200px;
}
</style>
